/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.tools;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.Buffer;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Austin
 */
public class MusExtract {

//    public static String cutMus(String url) {
//        String mus = "a";
//        if (url != null && !url.trim().equals("")) {
//            int biz = url.indexOf("biz");
//            int idx = url.indexOf("idx");
//            int end = url.indexOf("&", url.indexOf("idx"));
//            if (biz != -1 && idx != -1 && end != -1) {
//                mus = url.substring(url.indexOf("biz"), end);
//            }
//        }
//        return mus;
//    }

    public static String getMus(String url) throws MalformedURLException {
        String mus = "";
        if (url != null && !url.trim().equals("")) {
            URL u = new URL(url);
            String[] muss = u.getQuery().split("&");
            mus = muss[0].substring(muss[0].indexOf("=") + 1) + "_" + muss[1].substring(muss[1].indexOf("=") + 1) + "_" + muss[2].substring(muss[2].indexOf("=") + 1);
        }
        return mus;
    }

    public static void main(String[] args) throws IOException {
        try {
            // read file content from file
            StringBuffer sb = new StringBuffer("");

            FileReader reader = new FileReader("C:\\Users\\Austin\\Desktop\\WX.txt");
            BufferedReader br = new BufferedReader(reader);

            String str = null;

            while ((str = br.readLine()) != null) {
                sb.append(str + "/n");

                System.out.println(MusExtract.getMus(str));
            }
            
            br.close();
            reader.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
//        String xx = "http://mp.weixin.qq.com/s?__biz=MzIyNjIxNjUyNw==&mid=503433103&idx=6&sn=174e4ae0c658a1bd51411903b220d376&chksm=7386b8af44f131b950ac7e8e90a0214f34f108699e4caab5b1ae58d400227d88e4312135751b&scene=20#rd";
//        String xxx = "http://mp.weixin.qq.com/mp/appmsg/show?__biz=MjM5OTczMDMyMw==&appmsgid=10000213&itemidx=1&sign=1f353d11109c857c64f7597bf12c27dd&scene=20#wechat_redirect";
//        try {
//            URL url = new URL(xxx);
//            System.out.println(url.getQuery());
//        } catch (MalformedURLException ex) {
//            Logger.getLogger(UrlTest.class.getName()).log(Level.SEVERE, null, ex);
//        }

    }

}
